package com.wsoft.bpm.controller;

import com.wsoft.bpm.dto.ActProcessDTO;
import com.wsoft.bpm.service.ActProcessService;
import com.wsoft.bpm.vo.ProcessListVO;
import com.wsoft.response.ResponsePage;
import com.wsoft.response.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.activiti.engine.RepositoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author zhonglj
 * @date 2024-09-19 16:05
 **/
@RestController
@Tag(name = "工作流流程管理")
@RequestMapping("/bpm/process")
public class ActProcessController {
    @Resource
    private RepositoryService repositoryService;

    @Resource
    private ActProcessService actProcessService;

    @Operation(summary = "流程列表查询", description = "流程列表查询")
    @PostMapping("/list")
    public ResponseResult<ResponsePage<ProcessListVO>> list(@RequestBody ActProcessDTO dto) {
        return ResponseResult.success(ResponsePage.restPage(actProcessService.pageList(dto)));
    }

    @Operation(summary = "流程挂起", description = "流程挂起，挂起之后无法再启动该流程定义的实例，已经存在的流程实例不受影响！")
    @GetMapping("/suspend")
    public ResponseResult suspend(@RequestParam String processDefinitionId) {
        actProcessService.suspend(processDefinitionId);
        return ResponseResult.success("[sys.msg.success]");
    }

    @Operation(summary = "流程激活", description = "流程激活")
    @GetMapping("/active")
    public ResponseResult active(@RequestParam String processDefinitionId) {
        actProcessService.active(processDefinitionId);
        return ResponseResult.success("[sys.msg.success]");
    }

    @Operation(summary = "流程删除", description = "流程删除,默认非级联删除，只删除部署本身，保留其他关联数据!可以通过cascade=true来实现强制删除!")
    @GetMapping("/delete")
    public ResponseResult delete(@RequestParam(value = "id") String deploymentId,
                                 @RequestParam(value = "cascade", defaultValue = "false") boolean cascade) {
        repositoryService.deleteDeployment(deploymentId, cascade);
        return ResponseResult.success("[sys.msg.success]");
    }

    @Operation(summary = "模版映射", description = "将历史部署模型重置为当前模型数据")
    @GetMapping("/resetModel")
    public ResponseResult resetModel(@RequestParam String deploymentId, @RequestParam String fileName) {
        actProcessService.resetModel(deploymentId, fileName);
        return ResponseResult.success("[sys.msg.success]");
    }

    @Operation(summary = "xml预览", description = "预览指定部署版本xml")
    @GetMapping("/xmlView")
    public ResponseResult xmlView(@RequestParam String deploymentId, @RequestParam String fileName) {
        return ResponseResult.success(actProcessService.xmlView(deploymentId, fileName));
    }
}
